package com.example.bruger.androidneurosky;

import android.bluetooth.BluetoothAdapter;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.neurosky.thinkgear.TGDevice;
import com.neurosky.thinkgear.TGEegPower;

import java.io.Serializable;

/**
 * Created by kimmoandersen on 25/02/15.
 */

public class TGHandler extends Handler {
    int att, med, blinkCount = 0, heart;

    public TGDevice DeviceInfo() { return tgDevice; }

    private TGDevice tgDevice;
    BluetoothAdapter btAdapter;

    private static TGCallBack activity;

    public static void SetNewCallBack(TGCallBack callBack) {
        activity = callBack;
    }

    public boolean Start(TGCallBack callback) {
        activity = callback;
        btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter != null) {
            tgDevice = new TGDevice(btAdapter, this);
        }
        if (btAdapter != null) {
            tgDevice.connect(true);
            tgDevice.start();
            while (tgDevice.getState() != TGDevice.STATE_CONNECTED) {
                try {
                    Thread.sleep(250l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return true;
        }
        else
            return false;
    }

    @Override
    public void handleMessage(Message msg) {
        heart = 0;

        switch (msg.what) {
            case TGDevice.MSG_STATE_CHANGE:
                switch (msg.arg1) {
                    case TGDevice.STATE_IDLE:
                        break;
                    case TGDevice.STATE_CONNECTING:
                        break;
                    case TGDevice.STATE_CONNECTED:
                        tgDevice.start();
                        break;
                    case TGDevice.STATE_DISCONNECTED:
                        break;
                    case TGDevice.STATE_NOT_FOUND:
                        break;
                    case TGDevice.STATE_NOT_PAIRED:
                    default:
                        break;
                }
                break;
            case TGDevice.MSG_POOR_SIGNAL:
                Log.v("HelloEEG", "PoorSignal: " + msg.arg1);
                break;
            case TGDevice.MSG_ATTENTION:
                att = msg.arg1;
                Log.v("HelloEEG", "Attention: " + att);
                break;
            case TGDevice.MSG_MEDITATION:
                med = msg.arg1;
                Log.v("HelloEEG", "Meditation: " + med);
                break;
            case TGDevice.MSG_BLINK:
                Log.v("HelloEEG", "Blinked: " + msg.arg1);
                blinkCount = blinkCount++;
                break;
            case TGDevice.MSG_HEART_RATE:
                Log.v("HelloEEG", "Hearth rate: " + msg.arg1);
                heart = msg.arg1;
                break;
            case TGDevice.MSG_RAW_DATA:
                int rawValue = msg.arg1;
                break;
            case TGDevice.MSG_EEG_POWER:
                TGEegPower ep = (TGEegPower) msg.obj;
                Log.v("HelloEEG", "Delta: " + ep.delta);
                Log.v("HelloEEG", "Theta: " + ep.theta);
                Log.v("HelloEEG", "Low Alpha: " + ep.lowAlpha);
                Log.v("HelloEEG", "High Alpha: " + ep.highAlpha);
                Log.v("HelloEEG", "Low Beta: " + ep.lowBeta);
                Log.v("HelloEEG", "High Beta: " + ep.highBeta);
                Log.v("HelloEEG", "Low Gamma: " + ep.lowGamma);
                Log.v("HelloEEG", "Mid Gamma: " + ep.midGamma);

                activity.Update(att, med, ep.delta, ep.theta, ep.lowAlpha, ep.highAlpha,
                            ep.lowBeta, ep.highBeta, ep.lowGamma, ep.midGamma);
            default:
                break;
        }
    }
}
